package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanGoodsComment;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.TextUtil;

/**
 * Created by nero on 17-7-5.
 */

public class AdapterGoodsComment extends BaseAdapter {
    Context context;
    ArrayList<BeanGoodsComment> arrayList;

    public AdapterGoodsComment(Context context, ArrayList<BeanGoodsComment> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
//            view = LayoutInflater.from(context).inflate(R.layout.fragment_comment, null);
            view = LinearLayout.inflate(context, R.layout.fragment_comment, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        BeanGoodsComment beanGoodsComment = arrayList.get(i);

        holder.tv_time.setText(TextUtil.getText(beanGoodsComment.getAdd_time(), ""));
        holder.tv_nicknamae.setText(TextUtil.getText(beanGoodsComment.getUsername(), "匿名用户"));
        holder.tv_content.setText(TextUtil.getText(beanGoodsComment.getContent(), "暂无内容"));
        ImageUtils.BindImage(context, holder.iv_head, Constants.IMAGE_HOST + beanGoodsComment.getHead_pic(), 0);

        final ArrayList<String> arrayList_img = beanGoodsComment.getImg();
        if (arrayList_img != null && arrayList_img.size() > 0) {
            holder.gv_pic.setVisibility(View.VISIBLE);
            holder.gv_pic.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return arrayList_img.size();
                }

                @Override
                public Object getItem(int i) {
                    return i;
                }

                @Override
                public long getItemId(int i) {
                    return i;
                }

                @Override
                public View getView(int i, View view, ViewGroup viewGroup) {
                    View view_pic = LayoutInflater.from(context).inflate(R.layout.adapter_comment_pic, null);
                    ImageView imageView = view_pic.findViewById(R.id.iv_image);
                    ImageUtils.BindImage(context, imageView, arrayList_img.get(i), 0);
                    return view_pic;
                }
            });
        }
        return view;
    }


    class Holder {
        final ImageView iv_head;
        final TextView tv_nicknamae, tv_time, tv_content;
        final GridView gv_pic;

        public Holder(View root) {
            iv_head = root.findViewById(R.id.iv_head);
            tv_nicknamae = root.findViewById(R.id.tv_nicknamae);
            tv_time = root.findViewById(R.id.tv_time);
            tv_content = root.findViewById(R.id.tv_content);

            gv_pic = root.findViewById(R.id.gv_pic);

        }
    }
}
